<?php


namespace app\admin\controller\setting;


use app\admin\controller\AdminController;
use app\admin\service\setting\DbMysqlService;
use app\admin\validate\setting\DbMysqlValidate;
use think\response\Json;

class DbMysql extends AdminController
{
    /**
     * 获取表列表
     * @return Json
     */
    public function lists()
    {
        $params = $this->getData();
        return $this->success(DbMysqlService::getList($params));
    }

    /**
     * 表详情
     * @return Json
     */
    public function detail()
    {
        $params = (new DbMysqlValidate())->checkParam('detail');
        $result = DbMysqlService::detail($params['table_name']);
        return $this->data($result);
    }

    /**
     * 优化表
     * @return Json
     */
    public function optimize()
    {
        $params = (new DbMysqlValidate())->checkParam('optimize');
        $result =  DbMysqlService::optimize($params['tables']);
        if(true === $result) {
            return $this->success([], '优化成功');
        } else {
            return $this->error(DbMysqlService::getError());
        }
    }

    /**
     * 清理表碎片
     * @return Json
     */
    public function fragment()
    {
        $params = (new DbMysqlValidate())->checkParam('fragment');
        $result =  DbMysqlService::fragment($params['tables']);
        if(true === $result) {
            return $this->success([], '清理成功');
        } else {
            return $this->error(DbMysqlService::getError());
        }
    }

}